package com.example.ble_measure_plugin.utils

import android.R
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import com.contec.htd.code.base.ContecDevice
import com.contec.htd.code.callback.BluetoothSearchCallback
import com.contec.htd.code.callback.CommunicateCallback
import com.contec.htd.code.connect.ContecSdk
import com.contec.htd.code.device.ResultData
import java.lang.ref.WeakReference


/**
 *@packageName com.example.ble_measure_plugin.utils
 *@author kzcai
 *@date 2024/1/2
 */
class KangtaiTempUtils {

    val ktSdk = ContecSdk()

    private var hasFound = false

    private var context:WeakReference<Context>?=null
    companion object{
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED){
            KangtaiTempUtils()
        }

        private val TAG = "KangtaiTempUtils"

        private val SEARCH_SUCCESS = 1
        private val SEARCH_FAIL = 0
        private val SEARCH_COMPLETE = -1

        /**
         * @Description 设备名称
         * @Param
         * @return
         **/
        private const val DEVICE_BLUETOOTH_NAME = "HC-08"
        private const val DEVICE_BLUETOOTH_NAME_04 = "TEMP04"
        private const val DEVICE_BLUETOOTH_NAME_05 = "TEMP05" //HTD8808E/HTD8808C/TP700
        private const val DEVICE_BLUETOOTH_NAME_06 = "TEMP06"
        private const val DEVICE_BLUETOOTH_NAME_Bioland = "Bioland-IT"

    }

    fun initContext(context: Context){
        this.context = WeakReference(context)
    }

    fun startSearch(duration:Int){
        if (context==null){
            throw Exception("context is null")
        }
        ktSdk.startBluetoothSearch( searchCallback ,duration)
    }

    private val searchCallback = object : BluetoothSearchCallback{
        override fun onContecDeviceFound(p0: ContecDevice?) {
            if (p0!=null){
                val msg = Message()
                msg.what = SEARCH_SUCCESS
                msg.obj = p0
                handler.sendMessage(msg)
            }
        }

        override fun onSearchError(p0: Int) {
            val msg = Message()
            msg.what = SEARCH_FAIL
            msg.obj = p0
            handler.sendMessage(msg)
        }

        override fun onSearchComplete() {
            val msg = Message()
            msg.what = SEARCH_COMPLETE
            handler.sendMessage(msg)
        }

    }

    private val handler = object: Handler(Looper.myLooper()!!){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when(msg.what){
                SEARCH_SUCCESS->{
                    val device = msg.obj as ContecDevice
                    val name = device.name
                    if (name.contains(DEVICE_BLUETOOTH_NAME) || name.contains(DEVICE_BLUETOOTH_NAME_04)  || name.contains(DEVICE_BLUETOOTH_NAME_06)||name.contains(DEVICE_BLUETOOTH_NAME_Bioland)){
                        Log.e(TAG, "foundDeviceSuccess: 1", )
                        hasFound = true
                        /*关闭搜索*/
                        ktSdk.stopBluetoothSearch()
                        /*开始连接*/
                        ktSdk.startCommunicate(context!!.get()!!,device,communicateCallback)
                    }else if (name.contains(DEVICE_BLUETOOTH_NAME_05)){
                        Log.e(TAG, "foundDeviceSuccess: 2", )
                        hasFound = true
                        /*关闭搜索*/
                        ktSdk.stopBluetoothSearch()
                        val record: ByteArray = device.getRecord()
                        val recordStr = String(record)
                        val hasNewData = recordStr.contains("DATA")
//                        if (hasNewData) {
//
//                        } else {
//
//                        }
                        ktSdk.startCommunicate(context!!.get()!!,device,communicateCallback)
                    }
                }
                SEARCH_FAIL->{
                    val errorCode = msg.obj as Int
                }
                SEARCH_COMPLETE->{

                }
            }
        }
    }

    private val communicateCallback = object :CommunicateCallback{
        override fun onCommunicateSuccess(p0: ResultData?) {
            Log.e(TAG, "onCommunicateSuccess: state== ${p0!!.retValState}\n temp===${p0!!.temp}\nnumber===${p0.number}", )
        }

        override fun onCommunicateFailed(p0: Int) {
            Log.e(TAG, "onCommunicateFailed: ${p0}", )
        }

        override fun onCommunicateProgress(p0: Int) {
            Log.e(TAG, "onCommunicateProgress: ${p0}", )
        }

    }
}